package com.smallkingson.ylib.core.utils.test;

import android.os.Environment;
import com.smallkingson.ylib.core.Yr;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONArray;

public class FrameWriter {

  FileOutputStream out;
  private FileWriter byteLenOut;
  String name = "a_frame_" + new SimpleDateFormat("yyyy_MM_dd__HH-mm-ss").format(new Date
      ()) + ".file";
  String byteLen = "a_frame_" + new SimpleDateFormat("yyyy_MM_dd__HH-mm-ss").format(new Date
      ()) + ".len";
  JSONArray jsonArray = new JSONArray();
  int frameCount = 300;
  int currFrame;

  public void write(byte[] buf) {

    try {
      if (out == null) {
        out = new FileOutputStream(
            Environment.getExternalStorageDirectory()
                + "/" + name);
        byteLenOut = new FileWriter(
            new File(Environment.getExternalStorageDirectory()
                + "/" + byteLen));
      }
      currFrame++;
      Yr.d("写出帧数", currFrame);
      if (currFrame <= frameCount) {
        out.write(buf, 0, buf.length);
        jsonArray.put(buf.length);
      }
      if (currFrame > frameCount) {
        out.close();
        byteLenOut.write(jsonArray.toString());
        byteLenOut.close();
        Yr.d("关闭");
      }
    } catch (Exception e) {
      Yr.logError(e);
    }

//            new Date().get

//            out.w
  }
}
